/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Principal.java
 *
 * Created on 11-jul-2011, 16:06:17
 */
package Principal;

import java.io.File;
import java.util.ArrayList;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.jvnet.substance.SubstanceDefaultLookAndFeel;
//<editor-fold defaultstate="collapsed" desc="comment">

//</editor-fold>
import proyecto.Binarios;
import proyecto.Lectura;
import proyecto.Nodo;
import proyecto.NodoAt;
import proyecto.NodoEntidad;
import proyecto.formularios.CreateForm;

/**
 *
 */
public class Principal extends javax.swing.JFrame {

    /** Creates new form Principal */
    public Principal() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        SubstanceDefaultLookAndFeel.setSkin("org.jvnet.substance.skin.SubstanceSaharaLookAndFeel");
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        cEntidades = new javax.swing.JComboBox();
        rSelectodo = new javax.swing.JRadioButton();
        rSelec = new javax.swing.JRadioButton();
        bAdd = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setFont(new java.awt.Font("Maiandra GD", 1, 14));
        jButton1.setIcon(new javax.swing.ImageIcon("C:\\Documents and Settings\\Victor\\My Documents\\My Pictures\\2685\\0335.png")); // NOI18N
        jButton1.setText("BUSCAR DIRECTORIO");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Copperplate Gothic Light", 1, 24));
        jLabel1.setText("FRAMEWORK");

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setFont(new java.awt.Font("Maiandra GD", 0, 12));
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jLabel2.setFont(new java.awt.Font("Maiandra GD", 1, 12));
        jLabel2.setText("ENTIDADES DENTRO DEL PROYECTO");

        cEntidades.setFont(new java.awt.Font("Maiandra GD", 0, 12));

        rSelectodo.setFont(new java.awt.Font("Maiandra GD", 0, 12));
        rSelectodo.setText("Seleccionar Todo");
        rSelectodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rSelectodoActionPerformed(evt);
            }
        });

        rSelec.setFont(new java.awt.Font("Maiandra GD", 0, 12));
        rSelec.setText("Seleccionar Algunas");
        rSelec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rSelecActionPerformed(evt);
            }
        });

        bAdd.setFont(new java.awt.Font("Maiandra GD", 1, 12));
        bAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Principal/2.png"))); // NOI18N
        bAdd.setText("AGREGAR");
        bAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAddActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Maiandra GD", 1, 14));
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Principal/1.png"))); // NOI18N
        jButton2.setText("GENERAR");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(251, 251, 251)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(408, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(133, 133, 133)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)
                            .addComponent(cEntidades, 0, 483, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(177, 177, 177)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rSelec)
                            .addComponent(rSelectodo))
                        .addGap(312, 312, 312)))
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bAdd)
                        .addGap(44, 44, 44))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addGap(242, 242, 242)
                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
                .addGap(258, 258, 258))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(jLabel1)
                .addGap(30, 30, 30)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(rSelectodo)
                .addGap(18, 18, 18)
                .addComponent(rSelec)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cEntidades, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(72, 72, 72)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(67, 67, 67))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
      JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        
        String[] ficheros ;
        JDialog.setDefaultLookAndFeelDecorated(true);
        
        if(fc.showOpenDialog(fc) == JFileChooser.APPROVE_OPTION)
        {
        System.out.println("Directorio: " + fc.getSelectedFile().getAbsolutePath());
        File dir = new File(fc.getSelectedFile().getAbsolutePath());
        
        Lectura l = new Lectura(fc.getSelectedFile().getAbsolutePath()) ;
      //  l.GetFiles(dir);
        l.Busca(dir);
        
        atributos = l.atributos;
        System.out.print(atributos.get(0).GetName());
        entidades = l.entidades;
        path = l.path;
        entidad = l.GetEntidades();
        metodo = l.GetMetodos();
        System.out.print(metodo.get(0).GetName());
       
       
       // cf.CreaForm(cf.codForm, null)
        SetCombo(l.GetEntity());
        //jTextArea1.append("");
       // SetTable(l.GetData());
        System.out.println(path);
        Binarios.copia("jcalendar.jar", path+"/jcalendar.jar");
        
        
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void rSelectodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rSelectodoActionPerformed
         rSelec.setSelected(false);
         ArrayList<String> items = new ArrayList<String>();
         for(int x = 0;  x < cEntidades.getItemCount(); x++){
             seleccion.add(String.valueOf(cEntidades.getItemAt(x)));
             items.add(String.valueOf(cEntidades.getItemAt(x)));
            }
         jTextArea1.setText(" ");
         SetTe(items);
         bAdd.setEnabled(false);
         seleccion.clear();
         
          
    }//GEN-LAST:event_rSelectodoActionPerformed

    //Seleccion de las entridades... 
    private void rSelecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rSelecActionPerformed
       rSelectodo.setSelected(false);
       jTextArea1.setText(" ");
       bAdd.setEnabled(true);     
       seleccion.clear();
       
       
        
        
    }//GEN-LAST:event_rSelecActionPerformed

    private void bAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAddActionPerformed
            
        boolean f = false;
        if(seleccion.isEmpty()== true){
             jTextArea1.append(String.valueOf(cEntidades.getSelectedItem()) + "\n");
             seleccion.add(String.valueOf(cEntidades.getSelectedItem()));
        }
        else{
            for(int x = 0; x<seleccion.size(); x++){
                if(seleccion.get(x).contains(String.valueOf(cEntidades.getSelectedItem()))){
                    f = true;
                    x = seleccion.size();
                }
            }
            
            if(f == false){
                jTextArea1.append(String.valueOf(cEntidades.getSelectedItem()) + "\n");
             seleccion.add(String.valueOf(cEntidades.getSelectedItem()));
            }
            
            
        }
        
       
    }//GEN-LAST:event_bAddActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        
        System.out.println("seleccion"+seleccion.get(0));
        
        CreateForm cf = new CreateForm(atributos, entidades,path,metodo);
        cf.SetEntidad(entidad);
        cf.LeeJava();
        cf.CargaForm();
        cf.Formulario(seleccion);
        JOptionPane.showMessageDialog(null, "Se ha generado con éxito");
    }//GEN-LAST:event_jButton2ActionPerformed

    public void SetTe(ArrayList<String> nodo){
        for(int x = 0; x< nodo.size();  x++){
            jTextArea1.append(nodo.get(x)+ "\n");
            
        }
     }

    public void SetCombo(ArrayList<String> nodo){
        for(int i = 0; i< nodo.size(); i++){
             cEntidades.addItem(nodo.get(i)) ;
        }
      }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            
            
            public void run() {
//                 JFrame.setDefaultLookAndFeelDecorated(true);
//                 SubstanceLookAndFeel.setSkin("org.jvnet.substance.skin.NebulaSkin");         
                new Principal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAdd;
    private javax.swing.JComboBox cEntidades;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JRadioButton rSelec;
    private javax.swing.JRadioButton rSelectodo;
    // End of variables declaration//GEN-END:variables
    public ArrayList<String> seleccion = new ArrayList<String>();
    public ArrayList<NodoAt> atributos = new ArrayList<NodoAt>();
    public String path;
    public ArrayList<String> entidades = new ArrayList<String>();
    public ArrayList<NodoEntidad> entidad= new ArrayList<NodoEntidad>();
    public ArrayList<Nodo> metodo = new ArrayList<Nodo>();
    
}
